package com.bupt.ilink.controller;

import com.bupt.ilink.entity.DatasetTags;
import com.bupt.ilink.mapper.DatasetTagsMapper;
import com.bupt.ilink.service.DatasetTagsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@RestController
public class DatasetTagsController {
    private final DatasetTagsService datasetTagsService;
    private final DatasetTagsMapper datasetTagsMapper;

    @Autowired()
    public DatasetTagsController(DatasetTagsService datasetTagsService, DatasetTagsMapper datasetTagsMapper) {
        this.datasetTagsService = datasetTagsService;
        this.datasetTagsMapper = datasetTagsMapper;
    }

    public Map<Long, List<Map<String, String>>> getDistinctTagsByID() {
        List<DatasetTags> tagsList = datasetTagsMapper.getPublicDatasetTags();
        return tagsList.stream()
                .collect(Collectors.groupingBy(DatasetTags::getDatasetId,
                        Collectors.mapping(tagList -> {
                            Map<String, String> map = new HashMap<>();
                            map.put("tag_id", String.valueOf(tagList.getTagId()));
                            map.put("dataset_id", String.valueOf(tagList.getDatasetId()));
                            map.put("tag", tagList.getTag());
                            return map;
                        }, Collectors.toList())));
    }

    @GetMapping("/getPublicDatasetTags")
    public Map<Long, List<Map<String, String>>> getPublicDatasetTags() {
        System.out.println("公开数据集返回标签列表");
        System.out.println(getDistinctTagsByID());
        return getDistinctTagsByID();
    }
}